package com.rec.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.rec.entity.ConsumeLogs;
import com.rec.entity.Statistics;
import com.rec.repository.ConsumeLogsRepository;
import com.rec.repository.StatisticReposistory;
import com.rec.service.StatisticService;
import com.rec.utils.GetDayByDay;
@EnableScheduling
@Service
@Transactional
public class StatisticServiceImpl implements StatisticService{
	@Autowired
	private StatisticReposistory r;
	
	public Map<String, Object> all(Map<String, Object> params) {
		Specification<Statistics> spec = getSpec(params);
		// 创建分页对象
		Integer page = (Integer) params.get("page");
		Integer limit = (Integer) params.get("limit");
		Pageable pa = PageRequest.of(page, limit);
		Page<Statistics> result = r.findAll(spec,pa);
		List<Statistics> data = result.getContent();
		long count = result.getTotalElements();
		if(data.size()>0) {
			params.put("code", 0);
			params.put("data", data);
			params.put("count", count);
			
		} else {
			params.put("code", 1);
			params.put("msg", "服务器走丢了！");
		}
		return params;
	}
	private Specification<Statistics> getSpec(Map<String, Object> params) {
		return new Specification<Statistics>() {
			@Override
			public Predicate toPredicate(Root<Statistics> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate pre = cb.conjunction();
				List<Expression<Boolean>> sqlList = pre.getExpressions();
				if(!StringUtils.isEmpty(params.get("readroomid"))) {
					sqlList.add(cb.equal(root.get("readroomid"),params.get("readroomid")));
				}
				if(!StringUtils.isEmpty(params.get("begin"))) {
					sqlList.add(cb.greaterThan(root.get("time"),(String)params.get("begin")));
				}
				if(!StringUtils.isEmpty(params.get("end"))) {
					sqlList.add(cb.lessThan(root.get("time"),(String)params.get("end")));
				}
				return pre;
			}
			
		};
	}
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
	/**
	 * 每天晚上23：00，數據進行匯總添加到資源統計管理表   0 0 23 * * ? cron="0 0 23 * * ? "
	 */
	 @Autowired
	 private ConsumeLogsRepository conr;
	 @Scheduled(cron="0 0 23 * * ? ") 
	 public Integer resourceCount() {
		 Integer num = 0;
		 //new 一个统计数据
		 Statistics sta = new Statistics();
		 //获取前一天23时0分0秒
		 GetDayByDay be = new GetDayByDay();
		 String n = be.getSpecifiedDayBefore(df.format(new Date()));
		 //获取这一天的记录
		 List<ConsumeLogs> list = conr.getCountDay(n,df.format(new Date()));
		 for(int i = 0;i<list.size();i++) {
			 sta.setPeoplenums(Integer.parseInt(list.get(i).getCardno()));
			 sta.setReadroomid(list.get(i).getReadroomid());
			 sta.setTime(df.format(new Date()));
			 r.save(sta);
			 num++;
		 }
		 return num;
	 }
	 /**
	  * 测试方法
	  */
	@Override
	public List<ConsumeLogs> getTest(){
		 GetDayByDay be = new GetDayByDay();
		 String n = be.getSpecifiedDayBefore(df.format(new Date()));
		return conr.getCountDay(n,df.format(new Date()));
	}
}
